home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / hardware / i2clib38.lha / test_lib.c < prev   
Encoding:
C/C++ Source or Header  |  1994-05-24  |  2.3 KB  |  114 lines

  1. /*
  2. **     $Author: Bipsen $
  3. **     $Filename: test_lib.c $
  4. **     $Release: 1.00 $
  5. **     $Revision: 37.3 $
  6. **     $Date: 1994/01/07 20:24:10 $
  7. ** 
  8. **     Small testprogram for i2c.library
  9. **
  10. */
  11.  
  12. #include <exec/libraries.h>
  13. #include <libraries/dos.h>
  14. #include <stdio.h>
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17.  
  18. #include "i2c_library.h"
  19.  
  20. #define MaxNumI2C 200
  21.  
  22. #ifndef __SASC_60
  23. struct Library *I2C_Base=NULL;
  24. #endif
  25.  
  26. int main (void)
  27. {
  28.     UBYTE           i2cdata[MaxNumI2C];
  29.     int             count;
  30.     int             ErrorBack = RETURN_FAIL;
  31.  
  32.     char            kar;
  33.     int             i;
  34.  
  35. #ifndef __SASC_60
  36.     if(!(I2C_Base=OpenLibrary("i2c.library",37L)))
  37.         {
  38.         Printf("You need i2c.library v37+ in your LIBS: drawer!\n");
  39.         return(RETURN_ERROR);
  40.         }
  41. #endif
  42.  
  43.     if ((i=AllocI2C(DELAY_TIMER,"I2C_Test-program")))
  44.         {
  45.         switch(i)
  46.             {
  47.             case    I2C_BITS_BUSY:
  48.                         Printf ("Some bits in use, cannot allocate!\n");
  49.                         break;
  50.             case    I2C_NO_MISC_RESOURCE:
  51.                         Printf ("Cannot find misc.resource ???\n");
  52.                         break;
  53.             case    I2C_ERROR_PORT:
  54.                         Printf ("Cannot allocate port!\n");
  55.                         break;
  56.             case    I2C_ACTIVE:
  57.                         Printf ("I2C-bus already active!\n");
  58.                         break;
  59.             case    I2C_NO_TIMER:
  60.                         Printf ("Cannot open timer!\n");
  61.                         break;
  62.             }
  63.             fflush (0l);
  64.             ErrorBack = RETURN_ERROR;
  65.         }
  66.     else
  67.         {
  68.         /* I2C-bus is allocated, we can now do whatever we want */
  69.         /* with it                                              */
  70.         Printf("I2C-bus is succesfully allocated !\n");
  71.         fflush (0l);
  72.         ErrorBack = RETURN_OK;
  73.  
  74.         /* initialise port and set I2C-bus to default */
  75.         InitI2C ();
  76.  
  77.         SetDelay(1000);
  78.  
  79.         /* sending data  to I/O device at 0x40 (write-adres)   */
  80.         /* The I/O device is a PCF8574P                        */
  81.  
  82.         i2cdata[0] = 0xAA;
  83.  
  84.         for (i = 0; i < 5; ++i)
  85.             {
  86.             if (!SendI2C (0x40, 1, i2cdata))
  87.                 Printf ("Error sending data to device 0x40 ... \n");
  88.             i2cdata[0] = ~i2cdata[0];
  89.             }
  90.  
  91.  
  92.         /* receiving 10 bytes from the I/O device at 0x41 (read-adres) */
  93.  
  94.         if (!ReceiveI2C (0x41, 10, i2cdata))
  95.             Printf ("Error receiving data from device 0x41 ... \n");
  96.         else
  97.             for (count = 0; count < 10; count++)
  98.                 Printf ("Byte %2ld was %lx hex. \n", count + 1, i2cdata[count]);
  99.  
  100.  
  101.         Printf ("Press return to free port and deallocate I2C-bus\n");
  102.         fflush (0l);
  103.  
  104.         scanf ("%c", &kar);
  105.  
  106.         FreeI2C ();
  107.         }
  108. #ifndef __SASC_60
  109.     CloseLibrary(I2C_Base);
  110. #endif
  111.  
  112.     return (ErrorBack);
  113. }
  114.